<template>
	<view class="address">
		<view class="addressList">
			<view class="addressBox" v-for="(item,index) in list" :key="index">
				<view class="flex" @click="checkAddress(item)" style="align-items: center;">
					<image style="width:64rpx;height:64rpx;margin-right:16rpx;" src="/static/image/05_4cv.png"></image>
					<view style="width:580rpx;">
						<view class="flex" style="line-height:52rpx;">
							<view style="font-size: 30rpx;font-weight: bold;color: #333333;margin-right:12rpx;">
								{{item.username}}
							</view>
							<view style="font-size: 26rpx;color: #333333;">{{item.phone}}</view>
						</view>
						<view style="font-size: 26rpx;color: #666666;padding-bottom:16rpx;">
							{{item.area_detaile}}</view>
					</view>
				</view>
				<view class="flexJusp" v-if="type==1" style="padding-top:16rpx;border-top:2rpx solid #F0F0F0;">
					<view class="flex" @click="detail(item)">
						<image v-if="item.area_status==1" style="width:32rpx;height:32rpx;margin:4rpx 12rpx 0 0;"
							src="/static/image/05_4adf.png"></image>
						<image v-if="item.area_status==0" style="width:32rpx;height:32rpx;margin:4rpx 12rpx 0 0;"
							src="/static/image/05_4df.png"></image>
						<view style="font-size: 26rpx;line-height:40rpx;"
							:style="item.area_status==0?'color: #9C9C9C;':'color:#2089FF;'">设为默认地址</view>
					</view>

					<view class="flex">
						<image @click="delect(item)" style="width:40rpx;height:40rpx;" src="/static/image/05_4nchu.png">
						</image>
					</view>
				</view>
			</view>
		</view>
		<view class="buttons"  @click="addAddress">+ 新增服务地址</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				type: 1
			};
		},
		onLoad(option) {
			if (option.type) {
				this.type = option.type
			}
		},
		onShow() {
			this.loads()
		},
		methods: {
			checkAddress(item) {
				if (this.type == 1) {
					uni.navigateTo({
            url: '/pages_user/user/address/addAddress?id=' + item.address_id,
            events: {
              acceptDataFromOpenedPages: (data) => {
                this.loads()
              },
            },
					})
				} else {
					const eventChannel = this.getOpenerEventChannel()
					eventChannel.emit('acceptDataFromOpenedPages', item);
					uni.navigateBack()
				}
			},
			loads() {
				this.$httpapi('selectAddress', 'POST', {}).then(res => {
					this.list = res.data
				})
			},
			addAddress() {
				var that = this
				uni.navigateTo({
					url: '/pages_user/user/address/addAddress',
					events: {
						acceptDataFromOpenedPages: (data) => {
              this.loads()
						},
					},
				})
			},
			detail(item) {
				var that = this
				uni.showModal({
					title: '默认地址',
					content: '是否设置此地址为默认地址',
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
							that.$httpapi('updateAddress', 'POST', {
								addressId: item.address_id,
								opid:item.opid,
								areaStatus: 1
							}).then(ress => {
								uni.showToast({
									title: ress.msg,
									duration: 1500,
									icon: 'none'
								});
								that.loads()
							})
						} else if (res.cancel) {}
					}
				});
			},
			delect(item) {
				var that = this
				uni.showModal({
					title: '删除地址',
					content: '是否确定删除该地址',
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
							that.$httpapi('deleteAddress', 'POST', {
								addressId: item.address_id
							}).then(ress => {
								uni.showToast({
									title: ress.msg,
									duration: 1500,
									icon: 'none'
								});
								that.loads()
							})
						} else if (res.cancel) {}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F7F6FA;
	}

	.address {
		padding: 16rpx 0 200rpx;

		.addressList {
			width: 710rpx;
			margin: auto;

			.addressBox {
				margin-bottom: 20rpx;
				background-color: #FFF;
				border-radius: 12rpx;
				padding: 24rpx 28rpx 18rpx 30rpx;
			}
		}

		.buttons {
			width: 710rpx;
			height: 92rpx;
			background: linear-gradient(180deg, #2089FF 0%, #11ACEF 100%);
			border-radius: 52rpx;
			font-size: 28rpx;
			color: #FFFFFF;
			line-height: 92rpx;
			position: fixed;
			bottom: 100rpx;
			left: 30rpx;
			text-align: center;
		}
	}
</style>
